/**
 * 
 */
package com.gmail.blackdog1987.carcare.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.activity.AboatActivity;
import com.gmail.blackdog1987.carcare.activity.ContactActivity;
import com.umeng.analytics.MobclickAgent;
import com.umeng.fb.FeedbackAgent;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.umeng.update.UpdateStatus;

/**
 * @author wdy
 *
 */
public class SettingFragment extends Fragment implements OnClickListener {
	private View mContentView;
	final UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share");

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mContentView = inflater.inflate(R.layout.fragment_setting, null);
		reg();
		return mContentView;
	};

	private void reg() {
		// 首先在您的Activity中添加如下成员变量
		// 设置分享内容
		mController.setShareContent("车保姆分享内容");
		// 设置分享图片, 参数2为图片的url地址
		mController.setShareMedia(new UMImage(getActivity(), "http://www.umeng.com/images/pic/banner_module_social.png"));
		mController.getConfig().removePlatform(SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN);

		String appID = "wx11fb29d99020492b";
		String appSecret = "f17dee7c0735efe8a257c1c90f54ad1b";
		// 添加微信平台
		UMWXHandler wxHandler = new UMWXHandler(getActivity(), appID, appSecret);
		wxHandler.addToSocialSDK();
		// 添加微信朋友圈
		UMWXHandler wxCircleHandler = new UMWXHandler(getActivity(), appID, appSecret);
		wxCircleHandler.setToCircle(true);
		wxCircleHandler.addToSocialSDK();

		// 参数1为当前Activity，参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
		UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(getActivity(), "100424468", "c7394704798a158208a74ab60104f0ba");
		qqSsoHandler.addToSocialSDK();

		// 参数1为当前Activity，参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
		QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(getActivity(), "100424468", "c7394704798a158208a74ab60104f0ba");
		qZoneSsoHandler.addToSocialSDK();

		// 设置新浪SSO handler
		mController.getConfig().setSsoHandler(new SinaSsoHandler());

		// auto update
		UmengUpdateAgent.setUpdateAutoPopup(false);
		UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
			@Override
			public void onUpdateReturned(int updateStatus, UpdateResponse updateInfo) {
				switch (updateStatus) {
				case UpdateStatus.Yes: // has update
					UmengUpdateAgent.showUpdateDialog(getActivity(), updateInfo);
					break;
				case UpdateStatus.No: // has no update
					Toast.makeText(getActivity(), "没有更新", Toast.LENGTH_SHORT).show();
					break;
				case UpdateStatus.NoneWifi: // none wifi
					if(updateInfo!=null&&updateInfo.hasUpdate){
						UmengUpdateAgent.showUpdateDialog(getActivity(), updateInfo);

						Toast.makeText(getActivity(), "没有连结WIFI，继续更新将消耗您的流量", Toast.LENGTH_SHORT).show();
					}else{
						Toast.makeText(getActivity(), "没有更新", Toast.LENGTH_SHORT).show();
					}
					break;
				case UpdateStatus.Timeout: // time out
					Toast.makeText(getActivity(), "超时", Toast.LENGTH_SHORT).show();
					break;
				}
			}
		});

		mContentView.findViewById(R.id.setting_share).setOnClickListener(this);
		mContentView.findViewById(R.id.setting_feedback).setOnClickListener(this);
		mContentView.findViewById(R.id.setting_update).setOnClickListener(this);
		mContentView.findViewById(R.id.setting_about).setOnClickListener(this);
		mContentView.findViewById(R.id.setting_contact).setOnClickListener(this);
	}

	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart("MainScreen"); // 统计页面
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd("MainScreen");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.setting_contact:
			startActivity(new Intent(getActivity(), ContactActivity.class));
			break;
		case R.id.setting_share:
			mController.openShare(getActivity(), false);
			break;
		case R.id.setting_feedback:
			FeedbackAgent agent = new FeedbackAgent(getActivity());
			agent.startFeedbackActivity();
			break;
		case R.id.setting_update:
			UmengUpdateAgent.update(getActivity());
			break;
		case R.id.setting_about:
			startActivity(new Intent(getActivity(), AboatActivity.class));
			break;
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/** 使用SSO授权必须添加如下代码 */
		UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode);
		if (ssoHandler != null) {
			ssoHandler.authorizeCallBack(requestCode, resultCode, data);
		}
	}

}
